/*
 * @lc app=leetcode.cn id=101 lang=java
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    /**
     * Accepted
    198/198 cases passed (0 ms)
    Your runtime beats 100 % of java submissions
    Your memory usage beats 21.36 % of java submissions (40 MB)
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return true;
        return isSymmetricTree(root.left, root.right);
    }

    private boolean isSymmetricTree(TreeNode n1, TreeNode n2) {
        if (n1 == null && n2 == null)
            return true;
        else if (n1 == null || n2 == null)
            return false;
        else if (n1.val != n2.val)
            return false;
        else
            return isSymmetricTree(n1.left, n2.right) && isSymmetricTree(n1.right, n2.left);
    }
}
// @lc code=end
